﻿using System;
using System.Collections.Generic;
using IDAL.RF.Repair;
using Model;
using Model.RF.Repair;

namespace BLL.RF.Repair
{
    public class ProductTypeIDChange : BaseService<ProductTypeIDChangeInfo>
    {
        private static readonly IProductTypeIDChange dal = DALFactory.DataAccess.CreateProductTypeIDChange();

        public override void SetCurrentRepository()
        {
            CurrentRepository = dal;
        }

        public string InsertRecord(ProductTypeIDChangeInfo ptci)
        {
            ProductType p = new ProductType();
            PackageProduct pp = new PackageProduct();
            PackageProductInfo ppi = pp.GetPackageProductbyProductSN(ptci.ProductSN);
            string result;

            if (string.IsNullOrEmpty(ppi.ProductSN))
            {
                result = "没有关联过的序列号，无法进行产品类型替换！";
                return result;
            }
            if (!string.IsNullOrEmpty(ppi.IsPackage))
            {
                result = "已经包装过的序列号，不允许进行产品类型替换！";
                return result;
            }
            if (!ppi.ProductTypeID.Equals(ptci.OldProductTypeID, StringComparison.OrdinalIgnoreCase))
            {
                result = "序列号对应的旧产品类型不正确，请确认！";
                return result;
            }
            if (string.IsNullOrEmpty(p.LoadEntity(ptci.NewProductTypeID).ProductTypeID))
            {
                result = "新产品类型不存在，请确认！";
                return result;
            }
            try
            {
                Insert(ptci);
            }
            catch
            {
                result = "替换失败，请联系相关人员！";
                return result;
            }
            result = "替换成功！";
            return result;
        }
    }
}
